#!/usr/bin/env python

import os
import sys

curr_file = os.path.abspath(__file__)
if not curr_file.endswith('package_setup'):
  print 'Unexpected error occurred; wrong file called'
  sys.exit(1)

base_path = os.path.abspath(os.path.join(curr_file, '..'))
data_path = '%s/problem_data' % base_path

# Javascript

js_root = '%s/javascript' % base_path

# Install javascript dependencies (will fail if libgmp is not installed)
print ('Installing javascript dependencies. '
       'This will fail if libgmp is not installed.')
os.chdir(js_root)
cmd_bigint = 'npm install bigint'
os.system(cmd_bigint)
cmd_optimist = 'npm install optimist'
os.system(cmd_optimist)


js_path = '%s/path.js' % js_root
js_contents = "exports.DATA_PATH = '%s';\n" % data_path
with open(js_path, 'w') as fh:
  fh.write(js_contents)

# Python

py_path = '%s/python/path.py' % base_path
py_contents = "DATA_PATH = '%s'\n" % data_path
with open(py_path, 'w') as fh:
  fh.write(py_contents)

# Bash for Python path

export_stmt = ['\n',
               '# dhermes-project-euler adding to PYTHONPATH for imports\n',
               'export PYTHONPATH=$PYTHONPATH:%s\n' % base_path,
               '# dhermes-project-euler finished adding to PYTHONPATH\n']
bash_profile = os.path.join(os.path.expanduser('~'), '.bash_profile')
with open(bash_profile, 'a') as fh:
  fh.write(''.join(export_stmt))

source_cmd = 'source %s' % bash_profile
os.system(source_cmd)
